////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRay extends UTCTFGame;

var int Deaths;
var int BailterRayLives;
var bool bIsEasy;

event InitGame( string Options, out string ErrorMessage )
{
    local int temp;
    super.InitGame(Options, ErrorMessage);
    temp = GetIntOption( Options, "GameDifficulty", 2);
    if(temp == 0)
    {
        bIsEasy = true;
        BailterRayLives = 8;
    }
    if(temp == 1)
    {
        bIsEasy = false;
        BailterRayLives = 4;

    }
}


function bool CheckMaxLives(PlayerReplicationInfo Scorer)
{
    if(Deaths == BailterRayLives)
        return super.CheckMaxLives(Scorer);
    return true;

}

function bool CheckScore(PlayerReplicationInfo Scorer)
{

    if (Scorer.Team != None && Scorer.Team.Score == 1)
	{

		EndGame(Scorer,"LastMan");

		return true;
	}
	if (Deaths == BailterRayLives)
	{
	     EndGame(Scorer,"LastMan");
	}
}

function EndGame(PlayerReplicationInfo Winner, string Reason )
{
    super.EndGame(Winner,Reason);
    //RestartGame();
}

function PlayEndOfMatchMessage(){}

function ScoreKill(Controller Killer, Controller Other)
{
    local BailterRayPawn P;
    foreach DynamicActors(class'BailterRayPawn', P)
    {
         if(P.IsHumanControlled())
             break;
    }
	if( (killer == Other) || (killer == None) )
	{
    	if ( (Other!=None) && (Other.PlayerReplicationInfo != None) )
		{
            Deaths = Other.PlayerReplicationInfo.Deaths;
			Other.PlayerReplicationInfo.Score -= 400;
			Other.PlayerReplicationInfo.bForceNetUpdate = true;
		}
	}
	else if ( Killer == P.Controller && Killer.PlayerReplicationInfo != None )
	{
        if(Other.Pawn.Tag == 'EvilRobot')
            Killer.PlayerReplicationInfo.Score += BailterRayPawn_EvilRobot(Other.Pawn).value;
        else if(Other.Pawn.Tag == 'BlueSadRobot')
            Killer.PlayerReplicationInfo.Score += BailterRayPawn_BlueSadRobot(Other.Pawn).value;
        else if(Other.Pawn.Tag == 'BailterRay_GooBossTurret')
		    Killer.PlayerReplicationInfo.Score += 1000;
        else
            Killer.PlayerReplicationInfo.Score += 100;
		Killer.PlayerReplicationInfo.bForceNetUpdate = TRUE;
		Killer.PlayerReplicationInfo.Kills++;
	}
	else if ( Other == P.Controller)
	{
	    Deaths = Other.PlayerReplicationInfo.Deaths;
	    Other.PlayerReplicationInfo.Score -= 400;
	  	Other.PlayerReplicationInfo.bForceNetUpdate = true;
	}

	if ( GameRulesModifiers != None )
		GameRulesModifiers.ScoreKill(Killer, Other);

    if ( (Killer != None) || (BailterRayLives > 0) )
	{
		CheckScore(Killer.PlayerReplicationInfo);
	}
}

function UTBot AddBot(optional string BotName, optional bool bUseTeamIndex, optional int TeamIndex)
{
	local UTBot NewBot;
	local int i;

	if (BotName == "")
	{
		i = ActiveBots.Find('bInUse', false);
		if (i != INDEX_NONE)
		{
			BotName = ActiveBots[i].BotName;
			ActiveBots[i].bInUse = true;
		}
	}
	else
	{
		i = ActiveBots.Find('BotName', BotName);
		if (i != INDEX_NONE)
		{
			ActiveBots[i].bInUse = true;
		}
	}

	NewBot = SpawnBot("EvilRobot", bUseTeamIndex, TeamIndex);
	if ( NewBot == None )
	{
		WarnInternal("Failed to spawn bot.");
		return none;
	}
	NewBot.PlayerReplicationInfo.PlayerID = GetNextPlayerID();
	NumBots++;
	if ( WorldInfo.NetMode == NM_Standalone )
	{
		RestartPlayer(NewBot);
	}
	else
	{
		NewBot.GotoState('Dead','MPStart');
	}

    return NewBot;
}

// Monitor killed messages for fraglimit
function Killed( Controller Killer, Controller KilledPlayer, Pawn KilledPawn, class<DamageType> damageType )
{
	local bool		bEnemyKill;
	local UTPlayerReplicationInfo KillerPRI, KilledPRI;
	local UTVehicle V;
	local UTPlayerController PC;
	local class<UTDamageType> UTDamageType;

	UTDamageType = class<UTDamageType>(DamageType);

	if ( UTBot(KilledPlayer) != None )
		UTBot(KilledPlayer).WasKilledBy(Killer);

	if ( Killer != None )
		KillerPRI = UTPlayerReplicationInfo(Killer.PlayerReplicationInfo);
	if ( KilledPlayer != None )
		KilledPRI = UTPlayerReplicationInfo(KilledPlayer.PlayerReplicationInfo);

	bEnemyKill = ( (KillerPRI != None) && (KillerPRI != KilledPRI) && (KilledPRI != None) && (!bTeamGame || (KillerPRI.Team != KilledPRI.Team)) );

	V = (Killer != None) ? UTVehicle(Killer.Pawn) : None;

	if ( bEnemyKill && V != None )
	{
		// UT3G Hack to set appropriate vehicle indices for Nemesis and Paladin,
		// because we cannot modify UTGameContent classes
		if ( class<UTDamageType>(damageType).default.KillStatsName == 'KILLS_NEMESISTURRET' )
		{
			V.VehicleIndex = 14;
		}
		else if ( class<UTDamageType>(damageType).default.KillStatsName == 'KILLS_PALADINGUN' ||
				  class<UTDamageType>(damageType).default.KillStatsName == 'KILLS_PALADINEXPLOSION' )
		{
			V.VehicleIndex = 15;
		}

		if ( V.VehicleIndex >= 0 )
		{
			PC = UTPlayerController(Killer);
			if (PC != None && IsMPOrHardBotsGame())
			{
				//`log("Killed an enemy with "$Killer.Pawn$" with VehicleIndex:"@V.VehicleIndex);
				PC.ClientUpdateAchievement(EUTA_VEHICLE_JackOfAllTrades, 1<<V.VehicleIndex);
			}
		}
	}

	if ( bEnemyKill && UTPlayerController(Killer) != None && WorldInfo.NetMode != NM_Standalone)
	{
		UTPlayerController(Killer).ClientUpdateGetALife();
	}

	if ( (KillerPRI != None) && UTVehicle(KilledPawn) != None )
	{
		KillerPRI.IncrementVehicleKillStat(UTVehicle(KilledPawn).GetVehicleKillStatName());
	}
	if ( KilledPRI != None )
	{
		KilledPRI.LastKillerPRI = KillerPRI;

		if ( class<UTDamageType>(DamageType) != None )
		{
			class<UTDamageType>(DamageType).static.ScoreKill(KillerPRI, KilledPRI, KilledPawn);
		}
		else
		{
			// assume it's some kind of environmental damage
			if ( (KillerPRI == KilledPRI) || (KillerPRI == None) )
			{
				KilledPRI.IncrementSuicideStat('SUICIDES_ENVIRONMENT');
			}
			else
			{
					KillerPRI.IncrementKillStat('KILLS_ENVIRONMENT');
				KilledPRI.IncrementDeathStat('DEATHS_ENVIRONMENT');
			}
		}
		if ( KilledPRI.Spree > 4 )
		{
			EndSpree(KillerPRI, KilledPRI);
		}
		else
		{
			KilledPRI.Spree = 0;
		}
		if ( KillerPRI != None )
		{
			KillerPRI.IncrementKills(bEnemyKill, UTDamageType);

			if ( bEnemyKill )
			{
				V = UTVehicle(KilledPawn);
				if ( KilledPRI.IsHero() )
				{
					KillerPRI.IncrementHeroMeter(6.0, UTDamageType);
				}
				if ( (V != None) && (V.HeroBonus > 0) )
				{
					KillerPRI.IncrementHeroMeter(V.HeroBonus, UTDamageType);
				}

				if ( !bFirstBlood )
				{
					bFirstBlood = True;
					KillerPRI.IncrementHeroMeter(1.0, UTDamageType);
					//BroadcastLocalizedMessage( class'UTFirstBloodMessage', 0, KillerPRI );
					KillerPRI.IncrementEventStat('EVENT_FIRSTBLOOD');
				}
			}
		}
	}
    super.Killed(Killer, KilledPlayer, KilledPawn, damageType);

    if ( ((WorldInfo.NetMode == NM_Standalone) || (SinglePlayerMissionID != INDEX_NONE)) && (PlayerController(KilledPlayer) != None) )
    {
		// tell bots not to get into nearby vehicles
		for ( V=VehicleList; V!=None; V=V.NextVehicle )
			if ( WorldInfo.GRI.OnSameTeam(KilledPlayer,V) )
				V.PlayerStartTime = 0;
	}
}

function ShowPathTo(PlayerController P, int TeamNum);

function NotifySpree(UTPlayerReplicationInfo Other, int num);

function EndSpree(UTPlayerReplicationInfo Killer, UTPlayerReplicationInfo Other);

//DefaultProperties
defaultproperties
{
   //Gametype
   Name="BailterRay"
   Acronym="BR"
   MapPrefixes(0)="BR"

   //Settings
   Deaths = 0;
   bNoCustomCharacters=false;
   BailterRayLives = 4

   //Classes
   DefaultInventory(0)=class'BailterRay.BailterRayWeap_ControlBox'
   DefaultInventory(1)=none
   DefaultPawnClass=Class'BailterRay.BailterRayPawn'
   PlayerControllerClass=Class'BailterRay.BailterRayPlayerController'
   DeathMessageClass=none
   HUDType=Class'BailterRay.BailterRayHUD'
   MidGameMenuTemplate=BailterRayUIScene_MidGameMenu'BailterRayUIScenes.ModScenes.BailterRayMidGameMenu'
}
